#include "cli.h"

void disp_card(card c) {
    log_card(stdout, c);
}

void disp_welcome() {
    log_welcome(stdout);
}

void disp_args(struct args args) {
    log_args(stdout, args);
}

void disp_shuffle(struct board* board) {
    log_shuffle(stdout, board);
}

void disp_init_result(card c) {
    log_init_result(stdout, c);
};

void disp_game_header() {
    log_game_header(stdout);
}

void disp_game_summary(struct player* players, struct board* board, struct args args) {
    log_game_summary(stdout, players, board, args);
}

void disp_turn_header(struct player* player, struct board* board) {
    log_turn_header(stdout, player, board);
}

void disp_player_cards(struct player* player) {
    log_player_cards(stdout, player);
}

void disp_draw_cards(struct player* player, int cards_count) {
    log_draw_cards(stdout, player, cards_count);
}

void disp_board_topn(struct board* board, int topn) {
    log_board_topn(stdout, board, topn);
}

void disp_player_played(struct board* board, int cards_count, int player_id) {
    log_player_played(stdout, board, cards_count, player_id);
}

void disp_cards(card* cards, int cards_count) {
    log_cards(stdout, cards, cards_count);
}

void disp_wait(char* info) {
    if (info != NULL) printf("%s\n", info);
    printf("Press Enter to Continue\n");
    fflush(stdout);
    getchar();
    disp_cls();
}

void disp_cls() {
#ifdef __unix__
    puts("\x1b[2J");
#else
    system("clear");
#endif
}

void disp_show_card(int player_id, card c) {
    log_show_card(stdout, player_id, c);
}

card read_card() {
    printf("Please choose a card: ");
    card ans = {0, 0};
    char temp[128];
    fflush(stdout);
    scanf("%s", temp);
    for (int i = 0; i < SUIT_COUNT; i++) {
        char suit[32];
        card_suit_name(suit, i);
        if (!strcmp(temp, suit)) {
            ans.order += i * RANK_COUNT;
        }
    }
    scanf("%s", temp);
    for (int i = 0; i < RANK_COUNT; i++) {
        char rank[32];
        card_rank_name(rank, i);
        if (!strcmp(temp, rank)) {
            ans.order += i;
        }
    }
    getchar();
    return ans;
}

void cli_main(struct player* players, struct board* board, struct args args) {
    for (int _round_idx = 0; _round_idx < args.r; _round_idx++) {
        play_one_round(players, board, args);
    }
}
